import re
import pandas as pd
from datetime import datetime
from pathlib import Path


def clean_price(price_str):
    """清洗价格数据"""
    if pd.isna(price_str) or str(price_str).strip() in ['无', '暂无', '']:
        return None
    try:
        return float(re.sub(r'[^\d.]', '', str(price_str)))
    except:
        return None


def clean_pub_date(date_str):
    """清洗出版日期"""
    if pd.isna(date_str) or str(date_str).strip() in ['无', '暂无', '']:
        return None

    date_str = str(date_str).strip()
    formats = [
        '%Y-%m-%d', '%Y/%m/%d', '%Y.%m.%d',
        '%Y年%m月%d日', '%Y年%m月',
        '%Y-%m', '%m/%d/%Y'
    ]

    for fmt in formats:
        try:
            return datetime.strptime(date_str, fmt).strftime('%Y-%m-%d')
        except:
            continue
    return None


def clean_comment_count(comment_str):
    """清洗评论数量"""
    if pd.isna(comment_str) or str(comment_str).strip() in ['无', '暂无', '']:
        return 0
    try:
        return int(re.sub(r'[^\d]', '', str(comment_str)))
    except:
        return 0


def clean_data(input_file, output_file):
    """兼容旧版pandas的清洗流程"""
    try:
        df = pd.read_csv(input_file, encoding='utf-8-sig')
    except:
        try:
            df = pd.read_csv(input_file, encoding='gbk')
        except Exception as e:
            print(f"文件读取失败: {str(e)}")
            return None

    print(f"原始数据量: {len(df)}条")

    # 1. 去重
    df = df.drop_duplicates()

    # 2. 关键字段处理
    if '书名' in df.columns:
        df = df[~df['书名'].isna()]
    if '价格' in df.columns:
        df = df[~df['价格'].isna()]

    # 3. 数据清洗
    if '价格' in df.columns:
        df['价格'] = df['价格'].apply(clean_price)
        # 兼容旧版的between替代方案
        df = df[(pd.to_numeric(df['价格'], errors='coerce') >= 1) &
                (pd.to_numeric(df['价格'], errors='coerce') <= 1000)]

    if '出版日期' in df.columns:
        df['出版日期'] = df['出版日期'].apply(clean_pub_date)

    if '评论数' in df.columns:
        df['评论数'] = df['评论数'].apply(clean_comment_count)

    print(f"清洗后数据量: {len(df)}条")

    # 保存结果
    try:
        Path(output_file).parent.mkdir(parents=True, exist_ok=True)
        df.to_csv(output_file, index=False, encoding='utf-8-sig')
        print(f"数据已保存到: {output_file}")
        return df
    except Exception as e:
        print(f"文件保存失败: {str(e)}")
        return None


if __name__ == '__main__':
    cleaned_data = clean_data('../dangdang_data.csv', 'dangdang_cleaned.csv')
    if cleaned_data is not None:
        print("\n清洗后数据样例:")
        print(cleaned_data.head(3))